#include<bits/stdc++.h>
#define ll long long
#define ull unsigned long long
using namespace std;

const int N = 1e6 + 10, INF = 0x3f3f3f3f;

int n;
vector<int> g[N];
int deep[N], son[N], tt;

void dfs(int x, int fa)
{
    int cnt = 0;
    deep[x] = deep[fa] + 1;
    for (int y : g[x]) {
        if (y == fa) continue;
        dfs(y, x);
        cnt++;
    }
    if (cnt == 0) son[++tt] = x;
}

int ksm(int a, int b)
{
    int res = 1;
    while (b) {
        if (b & 1) res = res * a;
        a = a * a;b >>= 1;
    }
    return res;
}

void solve()
{
    cin >> n;
    for (int i = 1;i < n;i++) {
        int x, y;cin >> x >> y;
        g[x].push_back(y);
        g[y].push_back(x);
    }

    pow(2, 1);

    if (tt > 2) { cout << 0 << '\n'; return; }
    if (tt == 1) { cout << ksm(2, n) << '\n';return; }
    if (tt == 2) {
        tt = 0;
        dfs(1, 0);
    }
}

signed main()
{
    // ios::sync_with_stdio(0);
    // cin.tie(0);cout.tie(0);
#define ONLINE_JUDGE
#ifndef ONLINE_JUDGE
    std::istringstream in(R"()");
    std::cin.rdbuf(in.rdbuf());
#endif
    int T = 1;
    cin >> T;
    for (int i = 1;i <= T;i++) {
        solve();
    }
}